﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Model;
using System.Data.SqlClient;
using System.Data;
using System.Xml;

namespace DAL
{
   public class UserInfoDAL
    {
        /// <summary>
        /// 根据条件获取用户信息
        /// </summary>
        /// <param name="model">请假对象</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">一页显示的条数</param>
        /// <param name="totalCount">查询的总条数</param>
        /// <returns></returns>
       public static List<UserListViewEntity> GetUserInfoList(UserListViewEntity model, int pageIndex, int pageSize, out int totalCount)
        {


            var userInfoList = new List<UserListViewEntity>();
            var parameterOut = new SqlParameter()
            {
                ParameterName = "@totalCount",
                SqlDbType = SqlDbType.BigInt,
                Direction = ParameterDirection.Output
            };
            SqlParameter[] searchParams = new SqlParameter[]{
               new SqlParameter("UserAccount",model.UserAccount),
               new SqlParameter("UserName",model.UserName),
               new SqlParameter("Title",model.Title),
               new SqlParameter("RoleId",model.RoleId),
               new SqlParameter("FactoryCode",model.FactoryCode),               
               new SqlParameter("PageSize",pageSize),
               new SqlParameter("PageIndex",pageIndex),
               parameterOut,
           };

            var dataTable = DBHelper.GetDataSet("USP_UserInfoView_GetPage", searchParams);
            //UserListViewEntity whereEntity = null;
            foreach (DataRow row in dataTable.Rows)
            {
                UserListViewEntity entity = new UserListViewEntity(row);
                userInfoList.Add(entity);
                //if (whereEntity!=null)
                //{
                //    if (whereEntity.Id == entity.Id && whereEntity.RoleId!=entity.RoleId)
                //    {
                //        whereEntity.Description += ","+entity.Description;
                //        continue;
                //    }
                //    else
                //    {
                //        userInfoList.Add(whereEntity);
                //    }
                //}
                //whereEntity = entity;                
            }

            totalCount = Convert.ToInt32(parameterOut.Value);
            return userInfoList;
        }
       /// <summary>
       /// 拿取xml节点值
       /// </summary>
       /// <param name="roleList"></param>
       /// <returns></returns>
       public static List<SecurityRoleEntity> GetRoleXml(List<SecurityRoleEntity> roleList)
       {
           List<SecurityRoleEntity> whereEntity = new List<SecurityRoleEntity>();
           foreach (SecurityRoleEntity role in roleList)
           {
               role.Description = GetXml(role.Description);
               whereEntity.Add(role);
           }
           return whereEntity;
       }

       public static string GetXml(string xml)
       {
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.LoadXml(xml);
           XmlNodeList nodes = xmlDoc.SelectNodes(@"/items/item");
           string description = string.Empty;
           foreach (XmlNode node in nodes)
           {
               //通过Attributes获得属性名字为culture的属性
               description = node.Attributes["value"].Value;
           }
           return description;
       }
       
       
    }
}
